package system.security;

import framework.defined.SystemDefined;
import framework.security.Account;
import framework.security.AuthException;
import framework.security.RegApproval;
import framework.utils.RequestUtil;
import system.entity.SysUser;

import java.io.Serializable;
import java.util.Date;

/**
 * 用户对象
 */
public class UserAccount implements Account, Serializable {

    private SysUser u;

    public UserAccount(SysUser sysUser) {
        this.u = sysUser;
        //
        if (sysUser.getRegApproval() != null && sysUser.getRegApproval() > -1) {
            RegApproval[] values = RegApproval.values();
            if (sysUser.getRegApproval() < values.length)
                this._regApproval = values[sysUser.getRegApproval()];
        }
        //
        if (u.getIsDisabled() == null)
            this._isEnabled = true;
        else
            this._isEnabled = SystemDefined.NO_VALUE.equals(u.getIsDisabled());
    }

    @Override
    public long getId() {
        return u.getId();
    }

    @Override
    public String getUsername() {
        return u.getUsername();
    }

    @Override
    public String getPassword() {
        return u.getPassword();
    }

    @Override
    public String getPasswordSalt() {
        return u.getPasswordSalt();
    }

    @Override
    public Date accountExpired() {
        return u.getExpiredTime();
    }

    @Override
    public Date passwordExpired() {
        return u.getPasswordExpired();
    }

    @Override
    public boolean passwordMustChanged() {
        return SystemDefined.YES_VALUE.equals(u.getPasswordNext());
    }

    private boolean _isEnabled = false;

    @Override
    public boolean isEnabled() {
        return _isEnabled;
    }

    private RegApproval _regApproval = RegApproval.Agree;

    @Override
    public RegApproval getRegApproval() {
        return this._regApproval;
    }

    @Override
    public String getName() {
        return u.getName();
    }

    /**
     * 检查状态
     */
    @Override
    public void statusCheck() throws RuntimeException {
        Date now = new Date();

//        if (!StringUtils.hasLength(this.getPassword()))
//            throw new AuthException(AuthCode.STATUS_UNAVAILABLE, AuthResult.PASSWORD_NO_SET, RequestUtil.getMessageDefault("security.noSetPassword", "user not set password"));

        boolean enabled = this.isEnabled();
        if (!enabled)
            throw new AuthException(AuthException.ACCOUNT_DISABLED, RequestUtil.getMessageDefault("security.userDisabled", "account is disabled"));

        boolean accountExpired = this.accountExpired() != null && this.accountExpired().getTime() < now.getTime();
        if (accountExpired)
            throw new AuthException(AuthException.ACCOUNT_EXPIRED, RequestUtil.getMessageDefault("security.userExpired", "account is expired"));

        boolean passwordExpired = this.passwordExpired() != null && this.passwordExpired().getTime() < now.getTime();
        if (passwordExpired)
            throw new AuthException(AuthException.PASSWORD_EXPIRED, RequestUtil.getMessageDefault("security.passwordExpired", "password is expired"));
    }
}
